/**
 *
 */
package com.moogene.saad.gameplay.city;

import com.moogene.android.InfoMediator;
import com.moogene.android.R;
import com.moogene.android.engine.animlite.Animlite;
import com.moogene.android.engine.animlite.ScaleAnim;
import com.moogene.android.engine.mmi.Book;
import com.moogene.android.engine.mmi.ImageLayer;
import com.moogene.android.engine.mmi.Layer;
import com.moogene.android.engine.mmi.layout.GirdLayout;
import com.moogene.android.engine.mmi.widget.IconButton;
import com.moogene.android.engine.mmi.widget.Widget;
import com.moogene.saad.sns.Friend;
import com.moogene.saad.sns.Session;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;

import java.util.List;

/**
 * @author 28850924
 *
 */
public class FriendsBarLayer extends Layer {

    final static int COL = 5;

    final static int SPLITER = 20;

    final static int PADDING = 10;

    int itemSize = 150;

    GravatorButton currentChecked = null;



    public FriendsBarLayer(int width, int height)  {
        super(0 , InfoMediator.getScreenHeight() - height,
                width,  height, Layer.HUD);

        itemSize = height;

        this.setLayout(new GirdLayout(1, -1, itemSize - PADDING * 2));

        List<Friend> friends = Session.getSession().getFriends();
        for (int i = 0; i < friends.size(); i++) {
            final Friend friend = friends.get(i);
            Bitmap gravator = friend.getGravator();
            if (gravator != null) {
                final GravatorButton friendButton = new GravatorButton(gravator, itemSize - PADDING * 2 , itemSize - PADDING * 2);
                addLayer(friendButton);
            }
        }
    }

    @Override
    protected boolean willfollowCameraWhenDrag(){
        return false;
    }

    @Override
    protected boolean canDragHorizontal() {
        return true;
    }

    @Override
    public void draw(Canvas canvas) {
        fillColor(canvas, 0x3FB7DCFF);
        super.draw(canvas);
    }

    /* (non-Javadoc)
     * @see com.moogene.android.engine.mmi.Layer#update()
     */
    @Override
    public void update() {
        super.update();
    }

    public class GravatorButton extends Widget implements Widget.OnClickListener {

        private Animlite bgAnim;
        private Animlite hoverAnim;
        Bitmap bg;
        Bitmap gravator;
        boolean isChecked = false;

        public GravatorButton(Bitmap gravator, int width, int height){
            this.width = width;
            this.height = height;
            this.gravator = gravator;
            bg = InfoMediator.getBitmap(R.drawable.grid_frame);
            hoverAnim = new Animlite(InfoMediator.getBitmap(R.drawable.stack_frame_gold));
            bgAnim = new Animlite(bg);

            this.setOnClickListener(this);
        }

        @Override
        public void update() {
            super.update();
            if (isHold()) {
                ScaleAnim sa = bgAnim.getScaleAnim();
                if (sa.isPaused()) {
                    if (sa.getCurrent() > 1.2) {
                        sa.start(1, -0.2);
                        sa.start(1, -0.2);
                    }
                }
            }
        }
        /*
         * (non-Javadoc)
         *
         * @see com.moogene.android.engine.mmi.Layer#draw(android.graphics.Canvas)
         */
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawBitmap(gravator, null, getDstRect(), null);
            if(isHold()){
                hoverAnim.draw(canvas, getDstRect());
            }
            if(isChecked){
                bgAnim.draw(canvas, getDstRect());
            }
        }

        /* (non-Javadoc)
         * @see com.moogene.android.engine.mmi.widget.Widget.OnClickListener#onClick(com.moogene.android.engine.mmi.widget.Widget)
         */
        @Override
        public void onClick(Widget v) {
            Log.v("saad", "onclick" + v.toString());
            if(FriendsBarLayer.this.currentChecked != null){
                currentChecked.isChecked = false;
            }
            isChecked = true;
            FriendsBarLayer.this.currentChecked = (GravatorButton)v;
        }
    }

}
